//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.wdg.common.reponsitory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.EntityManager;
import javax.persistence.Query;

public class Finder {
    private StringBuilder jpqlBuilder;
    private List<String> params;
    private List<Object> values;
    private int firstResult = 0;
    private int maxResults = 0;
    public static final String ROW_COUNT = "select count(*) ";
    public static final String FROM = "from";
    public static final String JPQL_FETCH = "fetch";
    public static final String ORDER_BY = "order";
    public static final Pattern ORDER_BY_PATTERN = Pattern.compile("\\border\\s+by\\b", 2);

    protected Finder() {
        this.jpqlBuilder = new StringBuilder();
    }

    protected Finder(String jpql) {
        this.jpqlBuilder = new StringBuilder(jpql);
    }

    public static Finder create() {
        return new Finder();
    }

    public static Finder create(String jpql) {
        return new Finder(jpql);
    }

    public Finder append(String jpql) {
        this.jpqlBuilder.append(jpql);
        return this;
    }

    public int getFirstResult() {
        return this.firstResult;
    }

    public void setFirstResult(int firstResult) {
        this.firstResult = firstResult;
    }

    public int getMaxResults() {
        return this.maxResults;
    }

    public void setMaxResults(int maxResults) {
        this.maxResults = maxResults;
    }

    private List<String> getParams() {
        if (this.params == null) {
            this.params = new ArrayList();
        }

        return this.params;
    }

    private List<Object> getValues() {
        if (this.values == null) {
            this.values = new ArrayList();
        }

        return this.values;
    }

    public String getOrigJpql() {
        return this.jpqlBuilder.toString();
    }

    public String getRowCountJpql() {
        String jpql = this.jpqlBuilder.toString();
        int fromIndex = jpql.toLowerCase().indexOf("from");
        jpql = jpql.substring(fromIndex);
        String rowCountJpql = jpql.replace("fetch", "");
        int orderByStartIndex = -1;

        for(Matcher m = ORDER_BY_PATTERN.matcher(rowCountJpql); m.find(); orderByStartIndex = m.start()) {
        }

        if (orderByStartIndex > 0) {
            rowCountJpql = rowCountJpql.substring(0, orderByStartIndex);
        }

        return "select count(*) " + rowCountJpql;
    }

    public Finder setParam(String param, Object value) {
        this.getParams().add(param);
        this.getValues().add(value);
        return this;
    }

    public Finder setParams(Map<String, Object> paramMap) {
        Iterator var2 = paramMap.entrySet().iterator();

        while(var2.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry)var2.next();
            this.setParam((String)entry.getKey(), entry.getValue());
        }

        return this;
    }

    public Query setParamsToQuery(Query query) {
        if (this.params != null) {
            for(int i = 0; i < this.params.size(); ++i) {
                query.setParameter((String)this.params.get(i), this.values.get(i));
            }
        }

        return query;
    }

    public Query createQuery(EntityManager em) {
        Query query = this.setParamsToQuery(em.createQuery(this.getOrigJpql()));
        if (this.getFirstResult() > 0) {
            query.setFirstResult(this.getFirstResult());
        }

        if (this.getMaxResults() > 0) {
            query.setMaxResults(this.getMaxResults());
        }

        return query;
    }
}
